var from_point;
var to_point;
var mapposition;
var longitude;
var	latitude;
var	mappos;
$(document).ready(function(){   
     setmap();
     //点击确定按钮
     $(".determine").click(function (){
          $('#keep_sceen', parent.document).hide();		
          $('#onebox_sceen', parent.document).hide();		
          $('#twobox_sceen', parent.document).hide();			
          //设置position 地理位置         
          $('#position', parent.document).val(mapposition);
		  $('#noset', parent.document).html('已设置地图位置：'+ mappos).css({"color":"#999"});
		  $('#coordinate_address', parent.document).val(mappos);
		  $('#longitude', parent.document).val(longitude);
          $('#latitude', parent.document).val(latitude);
     });	
     //点击取消按钮
     $(".cancel").click(function (){ 	 
          $('#keep_sceen', parent.document).hide();		
          $('#onebox_sceen', parent.document).hide();		
          $('#twobox_sceen', parent.document).hide();	
     });	
});	
//
function setmap(){
	 var areapos=$('#position', parent.document).val();
     if( areapos =='' || areapos ==' ' || areapos =='null' ||  areapos =='undefined'){
		 getMaps(set_long,set_lat,set_address);
		 $('#mright').html(set_address);
     }else{
          var areapos=$('#position', parent.document).val().split(',');
          getMaps(areapos[0],areapos[1],areapos[2]);
          $('#mright').html(areapos[2]);
     }
}

//定位按钮
function searchMap(){
	var searchVal = str_trim($('#seaVal').val());
	var seaVal = (searchVal == '') ? set_address : searchVal;
	$('#seaVal').val(searchVal); //去除空格
	searchcon(seaVal);
	return false;
}

//搜索以后的值
function searchcon(seaVal){
	var map = new BMap.Map("mapshow");                     // 创建Map实例	
		map.enableScrollWheelZoom();					//鼠标大小缩放
		map.addControl(new BMap.NavigationControl());	//显示大小滚轮条
	var point = new BMap.Point(set_long,set_lat);
		map.centerAndZoom(point, 15);					//以15的格式显示地图中心

	var local = new BMap.LocalSearch(map, {
		renderOptions:{map: map, panel:"mright"},
		pageCapacity:10
	});
	local.search(seaVal);	//要搜索的值	
	
	//搜索以后获取列表内容的值
	local.setInfoHtmlSetCallback(function(poi){  
		var marker =poi.marker;
		var pos = marker.getPosition();
								
		// 创建地理编码实例      
		var myGeo = new BMap.Geocoder();      
		// 根据坐标得到地址描述    
		myGeo.getLocation(new BMap.Point(pos.lng,pos.lat), function(rs){   
			var addComplete = rs.addressComponents;
			var newAdress=addComplete.district +  addComplete.street+ addComplete.streetNumber;	//获取地址 		
			getMaps(pos.lng,pos.lat,newAdress);
		});
	});  

	/*local.setSearchCompleteCallback(function(SearchResult){ 
		lng=  SearchResult.getPoi(0).point.lng;
		lat =  SearchResult.getPoi(0).point.lat;
		position = SearchResult.getPoi(0).address;
	});	
	
	local.setMarkersSetCallback(function(SearchResult){ 
		lng=  SearchResult.getPosition().point.lng;
		alert(lng);
	});*/
}


//绘制地图定位函数
function getMaps(lng,lat,position) {
	var map = new BMap.Map("mapshow");					//显示位置
		map.enableScrollWheelZoom();					//鼠标大小缩放
		map.addControl(new BMap.NavigationControl());	//显示大小滚轮条
	var point = new BMap.Point(lng,lat);
		map.centerAndZoom(point, 15);					//以15的格式显示地图中心店

				
	var markerls = new BMap.Marker(point);				//新建标注
	var infoWindow = new BMap.InfoWindow("<span style=\"font-size:14px;\">当前位置:"+position+"<br><span style=\"font-size:12px; line-height:24px;\">(提示:任意点击地图,选择您的位置)</span></span>"); 
		map.openInfoWindow(infoWindow,point);			//默认时，显示窗口信息	
		mapposition = lng+','+lat+','+position;
		mappos = position;
		longitude = lng;
		latitude = lat;

		//$('#position').val(lng+','+lat+','+position);	
		markerls.addEventListener("click", function() {
			map.openInfoWindow(infoWindow,point);
		});												//点击标注点时显示窗口信息	
											
		markerls.enableDragging(true); 					//启用地图鼠标拖拽
		map.addOverlay(markerls);						//添加标注点在地图上
		
	//创建地理编码
	var geoc = new BMap.Geocoder(); 
	map.addEventListener('click', function(e) {		//点击地图任意位置事件触发			
		map.removeOverlay(markerls);				//点击地图任意点时，删除对象地图上的markerls标注点
		markerls = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));  //新建地图markerls
		map.addOverlay(markerls);					//添加新的标注点markerls		
				
		var pt = e.point;							//获取新的经纬度
		geoc.getLocation(pt, function(rs){			//根据坐标得到地址描述			
			var addComp = rs.addressComponents;
			var address=addComp.district +  addComp.street+ addComp.streetNumber;	//获取地址
			//var address=rs.address;					//获取地址
			mapposition = pt.lng+','+pt.lat+','+address;
			mappos = address;
			longitude = pt.lng;
			latitude = pt.lat;
			//$('#position').val(pt.lng+','+pt.lat+','+address);
			var infoWindow_new = new BMap.InfoWindow("<span style=\"font-size:14px;\">当前位置:"+address+"<br><span style=\"font-size:12px; line-height:24px;\">(提示:任意点击地图,选择您的位置)</span></span>"); 
			map.openInfoWindow(infoWindow_new,new BMap.Point(pt.lng, pt.lat));				 
		 });  
	});  
}




